<?php
	
	/* Over'all 3D Slider Manager Script */
	
	global $wpdb;
	
	$db_name = "3d_slider"; 
	include (TEMPLATEPATH . '/framework/includes/get_'.$db_name.'_db.php');
	
	
	
	if($_POST["adding"]){
	
		/* adding new slider item */
		if($_POST["new_id"]){

			foreach($_POST["new_id"] as $id){
				
				$slider	 = $_POST["slider".$id];
				$title = $_POST["title".$id];
				$description = $_POST["description".$id];
				
				if($slider != "" or $title != "" or $description != ""){
			
					$insert = "INSERT INTO ". $table_name ." SET 
					src = '".$slider."', 
					title = '".$title."',
					description = '".$description."',
					orderby = '0'";
					$wpdb->query( $insert );

					
					$get_insert_id = $wpdb->insert_id;
					$upgrade_insert_id = "UPDATE ". $table_name ." SET 
					orderby = '".$get_insert_id."'
					WHERE id = $get_insert_id";
					
					$wpdb->query( $upgrade_insert_id );
					$mysqlresult = "added";
				
				}
			}
		
		}
		
		/* updating new slider item */
		if($_POST["update_id"]){
			
			foreach($_POST["update_id"] as $id){
			
				$slider	 = $_POST["slider".$id];
				$title = $_POST["title".$id];
				$description = $_POST["description".$id];

				$insert = "UPDATE ". $table_name ." SET 
				src = '".$slider."', 
				title = '".$title."',
				description = '".$description."'
				WHERE id = $id";
				$wpdb->query( $insert );
				$mysqlresult = "updated";
				
			}
			
		}

	}
	
	/* deleting new slider item */
	if($_GET["deleting"]){
		$id = $_GET["deleting"];
		$insert = "DELETE FROM ". $table_name ." WHERE id = '".$id."'";
		$wpdb->query( $insert );
		$mysqlresult = "deleted";
	}

	/* moving new slider item */
	if($_GET['mode']=="sortby" && $_GET['ID']!="" && $_GET['CID']!="" && $_GET['SORTBY']!="" && $_GET['CSORTBY']!=""){
		$sql_exec="UPDATE $table_name SET orderby = ".$_GET['CSORTBY']." WHERE ID=".$_GET['ID'];
		$wpdb->query( $sql_exec );
				
		$sql_exec="UPDATE $table_name SET orderby = ".$_GET['SORTBY']." WHERE ID=".$_GET['CID'];
		$wpdb->query( $sql_exec );
		$mysqlresult = "moved";
	}
	
	$array_count=0;
	$array_record=0;
	$clumns = $wpdb->get_results("SELECT * FROM $table_name ORDER BY orderby");
	foreach ($clumns as $clumn) {
		$rowid = $clumn->id;
		$roworder = $clumn->orderby;
		$array_id[$array_count]=$rowid;
		$array_sortby[$array_count]=$roworder;
		$array_count++;
		$array_record++;
	}
	$array_count=0;
	
	
	$slider_datas = $wpdb->get_results("SELECT * FROM $table_name ORDER BY orderby");

?>

<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/3d_slider.js"></script>


<div class="template_url" id="<?php echo bloginfo('template_url'); ?>"></div>
<div class="3dskin_width" id="<?php echo get_option('3d_slider_width', '900'); ?>"></div>
<div class="3dskin_height" id="<?php echo get_option('3d_slider_height', '350'); ?>"></div>

<!-- begin slider warp -->
<div class="wrap">

	<div id="icon-edit" class="icon32"><br /></div>
	<h2>3D Slider Manager</h2>

	<?php 
		if($mysqlresult or $_REQUEST["updated"]){
			
			if($_REQUEST["updated"]){
					$mysqlresult = "updated";
			}
	?>
	<div id="mysqlresult" class="updated" style="margin-bottom:0; height:30px;">
		<div style="float:left; display:table;">
			<p>Success, its has been <strong><i><?php echo $mysqlresult; ?></i></strong>.</p>
		</div>
		<div style="float:right">
			<p><a class="result_remove" href=""><img src="<?php echo bloginfo('template_url'); ?>/images/admin/delete.png" alt="" /></a></p>
		</div>
	</div>
	<?php
		}
	?>
	
	<div id="setHolder" style="display:none; margin-top:20px; margin-bottom:10px;">
		<?php include_once( TEMPLATEPATH."/framework/options/settings_".$db_name.".php"); ?>
	</div>
	
	
	<form action="admin.php?page=<?php echo $_REQUEST["page"]; ?>" method="post">
	
	
	<!-- begin slider table -->
	<table cellspacing="0" class="widefat" style="margin-top:20px;">
		<!-- begin add item button -->
		<tfoot>
			<tr>
				<th>
				
					<div style="display:table; float:left;">
						<div style="float:left;">
							<input type="image" name="adding" value=" " style="background:none; background:url(<?php echo bloginfo('template_url'); ?>/images/admin/save.png); border:0; cursor:pointer; width:32px; height:31px; display:table;"/>
						</div>
						
						<div class="savecss">
							<input type="submit" name="adding" value="SAVE ALL CHANGES"/>
						</div>
					</div>
					
					<div style="display:table; float:right; margin-right:10px;">
						<div style="float:left;">
							<img src="<?php echo bloginfo('template_url'); ?>/images/admin/slider_settings.png" alt="" />
						</div>
						
						<a class="slider_set"href="">
							<div class="savecss" style="padding-left:10px;">
								SLIDER SETTINGS
							</div>
						</a>
					</div>
				
				</th>
			
			</tr>
		</tfoot>
		<!-- end add item button -->

	</table>
	<!-- end slider table -->	

	
	<!-- begin slider table -->
	<table cellspacing="0" class="widefat" id="rowHolder">
		<thead>
			<tr>
				<th>ID</th>
				<th colspan="5">List of Slider Items</th>
			</tr>
		</thead>

		<tbody>
			<?php if(!$slider_datas){?>
				<tr class="pad">
					<td style="width:5px; text-align:center;">&nbsp;</td>
					<td style="width:100%;">There is not any item... Please watch <strong>HELP VIDEO</strong> before add new item</td>
				</tr>
			<?php }	?>
			<?php foreach ($slider_datas as $data) { ?>
			<tr class='pad <?php if($n == 0){ echo "alternate"; $n = 1; } else { $n = 0; } ?>' id="editactive<?php echo $data->id;?>">
			
				<td style="width:5px; text-align:center;">
					<strong><?php echo $data->id;?></strong>
				</td>
				
				<td class="looktop" style="position:relative;">
					<div class="livepreview<?php echo $data->id;?>" id="<?php echo $data->id;?>" style="background:url(<?php echo bloginfo('template_url'); ?>/images/admin/left_arrow.gif) top left no-repeat;  background-position: 0px 20px; text-align:left; display:none; position:absolute; margin-top:-15px; margin-left:60px; padding-left:5px;">
					
						<div style="border:5px solid #15161a; background:#15161a url(<?php echo bloginfo('template_url'); ?>/images/admin/loader.gif) left top no-repeat;  min-width:50px; min-height:42px;" class="liveimg<?php echo $data->id;?>" id="<?php echo $data->src;?>"></div>
						
					</div>
					<img class="livepic" id="<?php echo $data->id;?>" src="<?php echo bloginfo('template_url'); ?>/images/admin/look.png" alt="" />
				</td>

				<td>
					<p><?php echo $data->title;?></p>
				</td>
				
				
				<td style="width:25px; text-align:center;">
					<a href="" class="editme" id="<?php echo $data->id;?>"><img src="<?php echo bloginfo('template_url'); ?>/images/admin/edit.png" alt="" /></a>
				</td>
				
				<td style="width:75px; text-align:center;">
					
					<?php if($array_count!=0) { 
						echo "<a href='admin.php?page=$_REQUEST[page]&mode=sortby&ID=".$array_id[$array_count]."&CID=".$array_id[$array_count-1]."&SORTBY=".$array_sortby[$array_count]."&CSORTBY=".$array_sortby[$array_count-1]."'>"; ?>
						<img src="<?php echo bloginfo('template_url'); ?>/images/admin/up.png" alt="" /></a>
					<?php } ?>
					
					<?php if($array_count!=$array_record-1) { 
						echo "<a href='admin.php?page=$_REQUEST[page]&mode=sortby&ID=".$array_id[$array_count]."&CID=".$array_id[$array_count+1]."&SORTBY=".$array_sortby[$array_count]."&CSORTBY=".$array_sortby[$array_count+1]."'>"; ?>
						<img src="<?php echo bloginfo('template_url'); ?>/images/admin/down.png" alt="" /></a>
					<?php } ?>
					
				</td>
				
				<td style="width:25px; text-align:center;">
					<a href="?page=<?php echo $_REQUEST["page"];?>&deleting=<?php echo $data->id;?>" onclick="return confirm('Are you sure you want to delete?')"><img src="<?php echo bloginfo('template_url'); ?>/images/admin/delete.png" alt="" /></a>
				</td>	
				
			</tr>
			
			<tr class='<?php if($s == 0){ echo "alternate"; $s = 1; } else { $s = 0; } ?>'  style="display: none;" id="edit<?php echo $data->id;?>">
				<td colspan="6" style="padding:0; margin:0; border:0;">
					<table id="<?php echo $data->id;?>" class="slider_new_table">
						<tbody>
							<tr class="slider_new_tr">	
								
								<td style="width: 1%;" rowspan="2"><strong><?php echo $data->id;?></strong></td>
								
								<td style="width: 3%;">
 	
									<img alt="" src="<?php echo bloginfo('template_url'); ?>/images/admin/look.png">
								
								</td>
								
								<td style="width: 30%;" id="update_slider<?php echo $data->id;?>" class="types">
									<textarea type="text" name="slider<?php echo $data->id;?>" id="slider<?php echo $data->id;?>" class="upload_input new_slider" title="<?php echo $data->id;?>" style="width:95%;"/><?php echo $data->src;?></textarea>
								</td>
								
								<td style="width: 10%;">
									
									<div class="upload_tab">
									
									<a title="Add an Image" class="set_input thickbox" id="slider<?php echo $data->id;?>" href="media-upload.php?post_id=<?php echo $data->id;?>&amp;type=image&amp;TB_iframe=true&amp;width=640&amp;height=414">
									<img alt="" src="<?php echo bloginfo('template_url'); ?>/images/admin/upload.png">
									<br />
									<span style="float:left; display:table;"><span class="tablespan" id="skins<?php echo $data->id;?>">width: <strong><?php echo get_option('3d_slider_width', '900'); ?>px</strong><br />height: <strong><?php echo get_option('3d_slider_height', '350'); ?>px</strong></span></span>
									</a>
									</div>

								</td>
								
								<td style="width: 40%;">
									<textarea type="text" name="title<?php echo $data->id;?>" title="<?php echo $data->id;?>" style="width:95%;" class="new_slider up_skins<?php echo $data->id;?>" id="slider<?php echo $data->id;?>"/><?php echo $data->title;?></textarea>
								</td>
								
								<td style="width: 5%;">
									<img class="up_skins<?php echo $data->id;?>" alt="" src="<?php echo bloginfo('template_url'); ?>/images/admin/title.png">
								</td>
								
								<td style="width: 3%; text-align: center;" rowspan="2">
									<br /><br /><a class="removedit" id="<?php echo $data->id;?>" href=""><img alt="" src="<?php echo bloginfo('template_url'); ?>/images/admin/cancel.png"></a>
								</td>
							</tr>
							
							<tr class="slider_new_tr">
								<td style="width: 3%;"></td>
								
								<td style="width: 30%;"></td>
								
								<td style="width: 10%;"></td>
								
								<td style="width: 40%;">
									<textarea name="description<?php echo $data->id;?>" title="<?php echo $data->id;?>" style="width:95%;" class="new_slider up_skins<?php echo $data->id;?>" id="slider<?php echo $data->id;?>"><?php echo $data->description;?></textarea>
								</td>
								
								<td style="width: 5%;">
									<img class="up_skins<?php echo $data->id;?>" alt="" src="<?php echo bloginfo('template_url'); ?>/images/admin/content.png">
								</td>
								
							</tr>
							

						</tbody>
					</table>
				</td>
			</tr>	
			<?php $array_count++; } ?>
			

			<!-- begin new item table tr -->
			<tr class='alternate'>
				<td colspan="6" style="padding:0; margin:0; border:0;">
					<table cellspacing="0" cellpadding="0" style="width:100%;"  id="rowHolder">
						<tbody id="here">
					
						</tbody>
					</table>
				</td>
			</tr>	
			<!-- end new item table tr -->

		</tbody>
		
		<!-- begin add item button -->
		<tfoot>
			<tr style="height:75px;">
				<th colspan="6">
					<a id="addnew" href="">
						<div style="float:left;">
							<?php 
							
							$next_id = 0;
							foreach ($slider_datas as $data) { 
								$next_id = max($next_id, $data->id);
							}
							
							?>
							<div id="count" name="<?php	echo $next_id; ?>"></div>
							<img src="<?php echo bloginfo('template_url'); ?>/images/admin/add.png" alt="" />
						</div>
						<div style="float:left; margin-top:9px; margin-left:5px;">
							ADD NEW ITEM
						</div>
					</a>
					
				</th>
			</tr>
		</tfoot>
		<!-- end add item button -->

	</table>
	<!-- end slider table -->
	

	<!-- begin slider table -->
	<table cellspacing="0" class="widefat" style="margin-top:0px;">
		<!-- begin add item button -->
		<tfoot>
			<tr>
				<th>
				
					<div style="display:table; float:left;">
						<div style="float:left;">
							<input type="image" name="adding" value=" " style="background:none; background:url(<?php echo bloginfo('template_url'); ?>/images/admin/save.png); border:0; cursor:pointer; width:32px; height:31px; display:table;"/>
						</div>
						
						<div class="savecss">
							<input type="submit" name="adding" value="SAVE ALL CHANGES"/>
						</div>
					</div>
					
				</th>
			</tr>
		</tfoot>
		<!-- end add item button -->

	</table>
	<!-- end slider table -->		
		
	</form>

</div>
<!-- end slider warp -->
